<?php
/**
 * Created by PhpStorm.
 * User: JYuans
 * Date: 2019/12/2
 * Time: 14:12
 */
namespace Mhome\Controller;

use Common\Model\CommonModel;

class MidExamineController extends MhomeController {

    /**
     * 获取用户中层考评需要审核的列表
     */
    public function getUserMidExamineList(){
        $mid_ex_id_array = array();
        if($this->user_data['rank'] == 1 || $this->user_data['rank'] == 2) {
            $down_examine_data = M("MidExamineDownRelation")
                ->where(['examine_user_id' => $this->user_data['user_id']])
                ->getField("mid_ex_id,status");

            foreach($down_examine_data as $key=>$value){
                if(!isset($mid_ex_id_array[$key])){
                    $mid_ex_id_array[$key] = array();
                    $mid_ex_id_array[$key]['examine_count'] = 0;
                    $mid_ex_id_array[$key]['examine_complete_count'] = 0;
                }
                $mid_ex_id_array[$key]['examine_count']++;
                if($value == 1){
                    $mid_ex_id_array[$key]['examine_complete_count']++;
                }
            }
        }

        if($this->user_data['rank'] == 3 || $this->user_data['rank'] == 4) {
            $same_examine_data = M("MidExamineSameRelation")
                ->where(['examine_user_id' => $this->user_data['user_id']])
                ->getField("mid_ex_id,status");

            foreach($same_examine_data as $key=>$value){
                if(!isset($mid_ex_id_array[$key])){
                    $mid_ex_id_array[$key] = array();
                    $mid_ex_id_array[$key]['examine_count'] = 0;
                    $mid_ex_id_array[$key]['examine_complete_count'] = 0;
                }
                $mid_ex_id_array[$key]['examine_count']++;
                if($value == 1){
                    $mid_ex_id_array[$key]['examine_complete_count']++;
                }
            }
        }

        if($this->user_data['rank'] == 4 || $this->user_data['rank'] == 5) {
            $up_examine_data = M("MidExamineUpRelation")
                ->where(['examine_user_id' => $this->user_data['user_id']])
                ->getField("mid_ex_id,status");

            foreach($up_examine_data as $key=>$value){
                if(!isset($mid_ex_id_array[$key])){
                    $mid_ex_id_array[$key] = array();
                    $mid_ex_id_array[$key]['examine_count'] = 0;
                    $mid_ex_id_array[$key]['examine_complete_count'] = 0;
                }
                $mid_ex_id_array[$key]['examine_count']++;
                if($value == 1){
                    $mid_ex_id_array[$key]['examine_complete_count']++;
                }
            }
        }
        $mid_ex_id_list = array_keys($mid_ex_id_array);

        if(!$mid_ex_id_list){
            return [];
        }

        $field = "mid_ex_id,status,title,same_start_time,same_end_time,up_start_time,up_end_time,add_time";

        $where = array();
        $where['mid_ex_id'] = ['in',$mid_ex_id_list];
        $where['status'] = ['in','1,2'];

        $list_data = M("MidExamine")
            ->where($where)
            ->field($field)
            ->order("status,add_time desc")
            ->select();

        if(!$list_data){
            return [];
        }

        $current_time = time();
        $status_str_array = ["0"=>"未开始","1"=>"进行中","2"=>"已结束","3"=>"已考核"];
        foreach($list_data as $key=>$value){
            //0为未开始，1为进行中，2为已结束
            $examine_status = 0;
            if($this->user_data['rank'] == 1 || $this->user_data['rank'] == 2){
                $list_data[$key]['examine_time'] = changeDate($this->user_data['use_start_time'])."——".changeDate($this->user_data['use_end_time']);
            }else if($this->user_data['rank'] == 3 || $this->user_data['rank'] == 4){
                $list_data[$key]['examine_time'] = changeDate($value['same_start_time'])."——".changeDate($value['same_end_time']);
            }else{
                $list_data[$key]['examine_time'] = changeDate($value['up_start_time'])."——".changeDate($value['up_end_time']);
            }

            if($value['status'] == 2){          //如果该中层审核已结束，则直接标为结束
                $examine_status = 2;
            }else{
                if($value['status'] == 1 && $mid_ex_id_array[$value['mid_ex_id']]['examine_complete_count'] > 0){    //如果该用户已进行审核，则标为已审核
                    $examine_status = 3;
                }else{
                    if($this->user_data['rank'] == 1 || $this->user_data['rank'] == 2){
                        //临时账号
                        if($this->user_data['use_start_time'] < $current_time){
                            $examine_status = 1;
                        }
                        if($this->user_data['use_end_time'] < $current_time){
                            $examine_status = 2;
                        }
                    }else if($this->user_data['rank'] == 3 || $this->user_data['rank'] == 4){
                        //中层
/*                        if($value['same_start_time'] == 0 || $value['same_end_time'] == 0){
                            $examine_status = 0;
                            $list_data[$key]['examine_time'] = "";
                        }else{*/
                            if($value['same_start_time'] < $current_time){
                                $examine_status = 1;
                            }
                            if($value['same_end_time'] < $current_time){
                                $examine_status = 2;
                            }
                        //}
                    }else{
                        //高管
                        /*if($value['up_start_time'] == 0 || $value['up_end_time'] == 0){
                            $examine_status = 0;
                            $list_data[$key]['examine_time'] = "";
                        }else {*/
                            if ($value['up_start_time'] < $current_time) {
                                $examine_status = 1;
                            }
                            if ($value['up_end_time'] < $current_time) {
                                $examine_status = 2;
                            }
                        //}
                    }
                }
            }

            $list_data[$key]['status'] = $examine_status;
            $list_data[$key]['status_str'] = $status_str_array[$examine_status];

            $list_data[$key]['add_time'] = changeDateTime($value['add_time']);

            $list_data[$key]['examine_id'] = $value['mid_ex_id'];
            unset($list_data[$key]['mid_ex_id']);
            $list_data[$key]['examine_type'] = 1;

            //same_start_time,same_end_time,up_start_time,up_end_time
            unset($list_data[$key]['same_start_time']);
            unset($list_data[$key]['same_end_time']);
            unset($list_data[$key]['up_start_time']);
            unset($list_data[$key]['up_end_time']);
        }

        return $list_data;
    }

    /**
     * 获取考评详情接口
     */
    public function getMidExamineInfo(){
        $mid_ex_id = $_REQUEST['mid_ex_id'] ? $_REQUEST['mid_ex_id'] : $this->errorReturn("缺少中层考核ID");

        $check_examine = M("MidExamine")
            ->where(['mid_ex_id'=>$mid_ex_id])
            ->field("status,same_start_time,same_end_time,up_start_time,up_end_time")
            ->find();
        if(!$check_examine){
            $this->errorReturn("考评ID错误");
        }

        if($check_examine['status'] != 1){
            $this->errorReturn("考评已结束或未开始");
        }

        $current_time = time();

        if($this->user_data['rank'] == 3 || $this->user_data['rank'] == 4){
            if($check_examine['same_start_time'] == 0 || $check_examine['same_end_time'] == 0){
                $this->errorReturn("该考核未设定中层考核时间，请联系管理员");
            }else{
                if($check_examine['same_start_time'] > $current_time || $check_examine['same_end_time'] < $current_time){
                    $this->errorReturn("中层考核时间为：".changeDate($check_examine['same_start_time'])."到".changeDate($check_examine['same_end_time']));
                }
            }
        }

        if($this->user_data['rank'] == 5){
            if($check_examine['up_start_time'] == 0 || $check_examine['up_end_time'] == 0){
                $this->errorReturn("该考核未设定高层考核时间，请联系管理员");
            }else{
                if($check_examine['up_start_time'] > $current_time || $check_examine['up_end_time'] < $current_time){
                    $this->errorReturn("高层考核时间为：".changeDate($check_examine['up_start_time'])."到".changeDate($check_examine['up_end_time']));
                }
            }
        }

        $down_mid_user_id = array();
        $same_mid_user_id = array();
        $up_mid_user_id = array();
        if($this->user_data['rank'] == 1 || $this->user_data['rank'] == 2) {
            $down_mid_user_id = M("MidExamineDownRelation")
                ->where(['mid_ex_id'=>$mid_ex_id,'examine_user_id'=>$this->user_data['user_id']])
                ->getField("be_examine_user_id",true);
        }
        if($this->user_data['rank'] == 3 || $this->user_data['rank'] == 4) {
            $same_mid_user_id = M("MidExamineSameRelation")
                ->where(['mid_ex_id'=>$mid_ex_id,'examine_user_id'=>$this->user_data['user_id']])
                ->getField("be_examine_user_id",true);

        }
        if($this->user_data['rank'] == 4 || $this->user_data['rank'] == 5) {
            $up_mid_user_id = M("MidExamineUpRelation")
                ->where(['mid_ex_id'=>$mid_ex_id,'examine_user_id'=>$this->user_data['user_id']])
                ->getField("be_examine_user_id",true);
        }

        $be_examine_user_id_array = array_keys(($down_mid_user_id?array_flip($down_mid_user_id):[])+
            ($same_mid_user_id?array_flip($same_mid_user_id):[])+
            ($up_mid_user_id?array_flip($up_mid_user_id):[]));

        if(!$be_examine_user_id_array){
            $this->errorReturn("您没有该中层审核权限");
        }


        $user_list = M("User")
            ->where(['user_id'=>['in',$be_examine_user_id_array]])
            ->order("depart_id")
            ->field("user_id,depart_id,number,name,rank,station,station_type")
            ->select();

        if(!$user_list){
            $this->returnJson("无数据");
        }

        $depart_id_array = array_column($user_list,'depart_id');
        $depart_name_array = M("Department")
            ->where(['depart_id'=>['in',$depart_id_array]])
            ->getField("depart_id,name");

        $user_array1 = array();
        $user_array2 = array();
        $user_array3 = array();
        $user_array4 = array();
        $user_array5 = array();
        $user_array6 = array();
        $user_array7 = array();

        foreach($user_list as $key=>$value){
            $user_list[$key]['depart_name'] = $depart_name_array[$value['depart_id']];
            $user_list[$key]['rank'] = CommonModel::rank_str_array[$value['rank']];
            //职能-正职，职能副职，业务正职，业务副职，单位正职，单位副职
            switch ($value['station_type']){
                case "职能-正职":
                    $user_array1[] = $user_list[$key];
                    break;
                case "职能-副职":
                    $user_array2[] = $user_list[$key];
                    break;
                case "业务-正职":
                    $user_array3[] = $user_list[$key];
                    break;
                case "业务-副职":
                    $user_array4[] = $user_list[$key];
                    break;
                case "单位-正职":
                    $user_array5[] = $user_list[$key];
                    break;
                case "单位-副职":
                    $user_array6[] = $user_list[$key];
                    break;
                default:
                    $user_array7[] = $user_list[$key];
            }
        }

        $new_user_list = array_merge($user_array1,$user_array2,$user_array3,$user_array4,$user_array5,$user_array6,$user_array7);

        $examine_title_list = M("MidExamineItem")
            ->where(['mid_ex_id'=>$mid_ex_id])
            ->getField("title",true);

        $return_data = array();
        $return_data['user_list'] = $new_user_list;
        $return_data['examine_title_list'] = $examine_title_list;

        $this->successReturn("获取成功",$return_data);
    }

    /**
     * 提交考评数据接口
     */
    public function submitExamineDataAction(){
        $mid_ex_id = $_REQUEST['mid_ex_id'] ? $_REQUEST['mid_ex_id'] : $this->errorReturn("缺少中层考核ID");

        $examine_json = $_REQUEST['examine_json']?$_REQUEST['examine_json']:$this->errorReturn("请输入考核数据JSON");
        $examine_array = json_decode($examine_json,true);
        if(!is_array($examine_array)){
            $this->errorReturn("考核数据格式错误");
        }
        if(!$examine_array){
            $this->errorReturn("请输入考核数据");
        }

        $user_id_array = array_column($examine_array,0);
        $user_id_array = array_keys(array_flip($user_id_array));
        $check_user_id = M("User")
            ->where(['user_id'=>['in',$user_id_array]])
            ->count();

        if($check_user_id != count($user_id_array)){
            $this->errorReturn("被审核用户ID参数错误");
        }

        $examine_title_list = M("MidExamineItem")
            ->where(['mid_ex_id'=>$mid_ex_id])
            ->getField("title",true);
        if(!$examine_title_list){
            $this->errorReturn("中层审核ID错误");
        }
        $item_count = count($examine_title_list);
        foreach($examine_array as $key=>$value){
            if($item_count != count($value) - 1){
                $this->errorReturn("考核数据JSON，格式错误,指标数据量不符");
            }
        }

        $amount_score = $item_count*10;

        $model = M();
        $model->startTrans();

        if($this->user_data['rank'] == 1 || $this->user_data['rank'] == 2) {
            foreach ($examine_array as $key => $value) {
                //处理得分详细数据
                $examine_result = $value;
                unset($examine_result[0]);
                $examine_result = json_encode(array_values($examine_result));
                //计算总得分
                $score = 0;
                for($i=1;$i<count($value);$i++){
                    if($value[$i] < 0 || $value[$i] > 10){
                        $model->rollback();
                        $this->errorReturn("一个指标分数必须是0到10分");
                    }
                    $score = bcadd($score,$value[$i],4);
                }
                //更新评审数据
                $new_data = array();
                //status,examine_result,score,percentile_score
                $new_data['status'] = 1;
                $new_data['examine_time'] = time();
                $new_data['examine_result'] = $examine_result;
                $new_data['score'] = $score;
                $new_data['percentile_score'] = bcmul(bcdiv($score,$amount_score,4),100,4);

                $update_result = M("MidExamineDownRelation")
                    ->where(['mid_ex_id'=>$mid_ex_id,'be_examine_user_id'=>$value[0],'examine_user_id'=>$this->user_data['user_id']])
                    ->save($new_data);

                if(!$update_result){
                    $model->rollback();
                    $this->errorReturn("提交数据失败，请重新尝试");
                }
            }
        }else{
            foreach ($examine_array as $key => $value) {
                //处理得分详细数据
                $examine_result = $value;
                unset($examine_result[0]);
                $examine_result = json_encode(array_values($examine_result));
                //计算总得分
                $score = 0;
                for($i=1;$i<count($value);$i++){
                    if($value[$i] < 0 || $value[$i] > 10){
                        $model->rollback();
                        $this->errorReturn("一个指标分数必须到0到10分");
                    }
                    $score = bcadd($score,$value[$i],4);
                }

                //检查是否是同级评审，是则进行处理
                $check_same_relation = M("MidExamineSameRelation")
                    ->where(['mid_ex_id'=>$mid_ex_id,'be_examine_user_id'=>$value[0],'examine_user_id'=>$this->user_data['user_id']])
                    ->count();
                if($check_same_relation){
                    //存在同级互评关系，更新数据
                    $new_data = array();
                    //status,examine_result,score,percentile_score
                    $new_data['status'] = 1;
                    $new_data['examine_time'] = time();
                    $new_data['examine_result'] = $examine_result;
                    $new_data['score'] = $score;
                    $new_data['percentile_score'] = bcmul(bcdiv($score,$amount_score,4),100,4);

                    $update_result = M("MidExamineSameRelation")
                        ->where(['mid_ex_id'=>$mid_ex_id,'be_examine_user_id'=>$value[0],'examine_user_id'=>$this->user_data['user_id']])
                        ->save($new_data);

                    if(!$update_result){
                        $model->rollback();
                        $this->errorReturn("提交数据失败，请重新尝试");
                    }
                }

                //检查是否存在上级评审情况，如果该评审员为中层正职或者高管则有可能
                $check_up_relation = M("MidExamineUpRelation")
                    ->where(['mid_ex_id'=>$mid_ex_id,'be_examine_user_id'=>$value[0],'examine_user_id'=>$this->user_data['user_id']])
                    ->field("weight")
                    ->find();
                if($check_up_relation){
                    //存在上级评审关系，更新数据
                    $new_data = array();
                    $new_data['status'] = 1;
                    $new_data['examine_time'] = time();
                    $new_data['examine_result'] = $examine_result;
                    $new_data['score'] = $score;
                    $new_data['weight_score'] = bcmul($score,$check_up_relation['weight'],4);

                    $update_result = M("MidExamineUpRelation")
                        ->where(['mid_ex_id'=>$mid_ex_id,'be_examine_user_id'=>$value[0],'examine_user_id'=>$this->user_data['user_id']])
                        ->save($new_data);
                    if(!$update_result){
                        $model->rollback();
                        $this->errorReturn("提交数据失败，请重新尝试");
                    }
                }

                if(!$check_same_relation && !$check_up_relation){
                    $model->rollback();
                    $this->errorReturn("存在错误用户ID");
                }

            }
        }

        $model->commit();
        $this->successReturn("提交成功");
    }

    /**
     * 获取考评后详情接口
     */
    public function getMidExamineResultInfo(){
        $mid_ex_id = $_REQUEST['mid_ex_id'] ? $_REQUEST['mid_ex_id'] : $this->errorReturn("缺少中层考核ID");

        $check_examine = M("MidExamine")
            ->where(['mid_ex_id'=>$mid_ex_id])
            ->field("status,same_start_time,same_end_time,up_start_time,up_end_time")
            ->find();
        if(!$check_examine){
            $this->errorReturn("考评ID错误");
        }

        /*if($check_examine['status'] != 1){
            $this->errorReturn("考评已结束或未开始");
        }*/

        $current_time = time();

        if($this->user_data['rank'] == 3 || $this->user_data['rank'] == 4){
            if($check_examine['same_start_time'] == 0 || $check_examine['same_end_time'] == 0){
                $this->errorReturn("该考核未设定中层考核时间，请联系管理员");
            }else{
                if($check_examine['same_start_time'] > $current_time || $check_examine['same_end_time'] < $current_time){
                    $this->errorReturn("中层考核时间为：".changeDate($check_examine['same_start_time'])."到".changeDate($check_examine['same_end_time']));
                }
            }
        }

        if($this->user_data['rank'] == 5){
            if($check_examine['up_start_time'] == 0 || $check_examine['up_end_time'] == 0){
                $this->errorReturn("该考核未设定高层考核时间，请联系管理员");
            }else{
                if($check_examine['up_start_time'] > $current_time || $check_examine['up_end_time'] < $current_time){
                    $this->errorReturn("高层考核时间为：".changeDate($check_examine['up_start_time'])."到".changeDate($check_examine['up_end_time']));
                }
            }
        }

        $down_mid_user_id = array();
        $same_mid_user_id = array();
        $up_mid_user_id = array();

        $relation_array = array();
        if($this->user_data['rank'] == 1 || $this->user_data['rank'] == 2) {
            $down_mid_user_data = M("MidExamineDownRelation")
                ->where(['mid_ex_id'=>$mid_ex_id,'examine_user_id'=>$this->user_data['user_id']])
                ->getField("be_examine_user_id,examine_result,score");
            if($down_mid_user_data) {
                $relation_array = $down_mid_user_data;
            }

            $down_mid_user_id = array_keys($down_mid_user_data);
        }
        if($this->user_data['rank'] == 3 || $this->user_data['rank'] == 4) {
            $same_mid_user_data = M("MidExamineSameRelation")
                ->where(['mid_ex_id'=>$mid_ex_id,'examine_user_id'=>$this->user_data['user_id']])
                ->getField("be_examine_user_id,examine_result,score");
            if($same_mid_user_data){
                if($relation_array){
                    $relation_array = array_merge($relation_array,$same_mid_user_data);
                }else{
                    $relation_array = $same_mid_user_data;
                }
                $same_mid_user_id = array_keys($same_mid_user_data);
            }
        }
        if($this->user_data['rank'] == 4 || $this->user_data['rank'] == 5) {
            $up_mid_user_data = M("MidExamineUpRelation")
                ->where(['mid_ex_id'=>$mid_ex_id,'examine_user_id'=>$this->user_data['user_id']])
                ->getField("be_examine_user_id,examine_result,score");
            if($up_mid_user_data){
                if($relation_array){
                    $relation_array = array_merge($relation_array,$up_mid_user_data);
                }else{
                    $relation_array = $up_mid_user_data;
                }
                $up_mid_user_id = array_keys($up_mid_user_data);
            }
        }

        $be_examine_user_id_array = array_keys(($down_mid_user_id?array_flip($down_mid_user_id):[])+
            ($same_mid_user_id?array_flip($same_mid_user_id):[])+
            ($up_mid_user_id?array_flip($up_mid_user_id):[]));

        if(!$be_examine_user_id_array){
            $this->errorReturn("您没有该中层审核权限");
        }

        $user_list = M("User")
            ->where(['user_id'=>['in',$be_examine_user_id_array]])
            ->field("user_id,depart_id,number,name,rank,station,station_type")
            ->select();

        if(!$user_list){
            $this->returnJson("无数据");
        }

        $user_array1 = array();
        $user_array2 = array();
        $user_array3 = array();
        $user_array4 = array();
        $user_array5 = array();
        $user_array6 = array();
        $user_array7 = array();

        $depart_id_array = array_column($user_list,'depart_id');
        $depart_name_array = M("Department")
            ->where(['depart_id'=>['in',$depart_id_array]])
            ->getField("depart_id,name");

        foreach($user_list as $key=>$value){
            $user_list[$key]['depart_name'] = $depart_name_array[$value['depart_id']];
            $user_list[$key]['rank'] = CommonModel::rank_str_array[$value['rank']];

            $user_list[$key]['examine_result'] = json_decode($relation_array[$value['user_id']]['examine_result'],true);
            $user_list[$key]['score'] = floatval($relation_array[$value['user_id']]['score']);

            //职能-正职，职能副职，业务正职，业务副职，单位正职，单位副职
            switch ($value['station_type']){
                case "职能-正职":
                    $user_array1[] = $user_list[$key];
                    break;
                case "职能-副职":
                    $user_array2[] = $user_list[$key];
                    break;
                case "业务-正职":
                    $user_array3[] = $user_list[$key];
                    break;
                case "业务-副职":
                    $user_array4[] = $user_list[$key];
                    break;
                case "单位-正职":
                    $user_array5[] = $user_list[$key];
                    break;
                case "单位-副职":
                    $user_array6[] = $user_list[$key];
                    break;
                default:
                    $user_array7[] = $user_list[$key];
            }
        }

        $new_user_list = array_merge($user_array1,$user_array2,$user_array3,$user_array4,$user_array5,$user_array6,$user_array7);

        $examine_title_list = M("MidExamineItem")
            ->where(['mid_ex_id'=>$mid_ex_id])
            ->getField("title",true);

        $return_data = array();
        $return_data['user_list'] = $new_user_list;
        $return_data['examine_title_list'] = $examine_title_list;

        $this->successReturn("获取成功",$return_data);
    }

    /**
     * 获取用户需要审核的列表
     */
    /*public function getUserMidExamineList(){
        $mid_ex_id_array = array();
        if($this->user_data['rank'] == 1 || $this->user_data['rank'] == 2) {
            $down_examine_data = M("MidExamineDownRelation")
                ->where(['examine_user_id' => $this->user_data['user_id']])
                ->getField("mid_ex_id,status");

            foreach($down_examine_data as $key=>$value){
                if(!isset($mid_ex_id_array[$key])){
                    $mid_ex_id_array[$key] = array();
                    $mid_ex_id_array[$key]['examine_count'] = 0;
                    $mid_ex_id_array[$key]['examine_complete_count'] = 0;
                }
                $mid_ex_id_array[$key]['examine_count']++;
                if($value == 1){
                    $mid_ex_id_array[$key]['examine_complete_count']++;
                }
            }
        }

        if($this->user_data['rank'] == 3 || $this->user_data['rank'] == 4) {
            $same_examine_data = M("MidExamineSameRelation")
                ->where(['examine_user_id' => $this->user_data['user_id']])
                ->getField("mid_ex_id,status");

            foreach($same_examine_data as $key=>$value){
                if(!isset($mid_ex_id_array[$key])){
                    $mid_ex_id_array[$key] = array();
                    $mid_ex_id_array[$key]['examine_count'] = 0;
                    $mid_ex_id_array[$key]['examine_complete_count'] = 0;
                }
                $mid_ex_id_array[$key]['examine_count']++;
                if($value == 1){
                    $mid_ex_id_array[$key]['examine_complete_count']++;
                }
            }
        }

        if($this->user_data['rank'] == 4 || $this->user_data['rank'] == 5) {
            $up_examine_data = M("MidExamineUpRelation")
                ->where(['examine_user_id' => $this->user_data['user_id']])
                ->getField("mid_ex_id,status");

            foreach($up_examine_data as $key=>$value){
                if(!isset($mid_ex_id_array[$key])){
                    $mid_ex_id_array[$key] = array();
                    $mid_ex_id_array[$key]['examine_count'] = 0;
                    $mid_ex_id_array[$key]['examine_complete_count'] = 0;
                }
                $mid_ex_id_array[$key]['examine_count']++;
                if($value == 1){
                    $mid_ex_id_array[$key]['examine_complete_count']++;
                }
            }
        }
        $mid_ex_id_list = array_keys($mid_ex_id_array);

        if(!$mid_ex_id_list){
            $this->returnJson(503,"无数据1");
        }

        $field = "mid_ex_id,status,title,same_start_time,same_end_time,up_start_time,up_end_time,add_time";

        $where = array();
        $where['mid_ex_id'] = ['in',$mid_ex_id_list];
        $where['status'] = ['in','1,2'];

        $list_data = M("MidExamine")
            ->where($where)
            ->field($field)
            ->order("status")
            ->select();

        if(!$list_data){
            $this->returnJson(503,"无数据2");
        }

        $current_time = time();
        $status_str_array = ["0"=>"未开始","1"=>"进行中","2"=>"已结束","3"=>"已考核"];
        foreach($list_data as $key=>$value){
            //0为未开始，1为进行中，2为已结束
            $examine_status = 0;

            if($value['status'] == 2){          //如果该中层审核已结束，则直接标为结束
                $examine_status = 2;
            }else{
                if($value['status'] == 1 && $mid_ex_id_array[$value['mid_ex_id']]['examine_complete_count'] > 0){    //如果该用户已进行审核，则标为已审核
                    $examine_status = 3;
                }else{
                    if($this->user_data['rank'] == 1 || $this->user_data['rank'] == 2){
                        if($this->user_data['use_start_time'] < $current_time){
                            $examine_status = 1;
                        }
                        if($this->user_data['use_end_time'] < $current_time){
                            $examine_status = 2;
                        }
                        //临时账号
                        $list_data[$key]['examine_time'] = changeDate($this->user_data['use_start_time'])."---".changeDate($this->user_data['use_end_time']);
                    }else if($this->user_data['rank'] == 3 || $this->user_data['rank'] == 4){
                        //中层
                        if($value['same_start_time'] == 0 || $value['same_end_time'] == 0){
                            $examine_status = 0;
                            $list_data[$key]['examine_time'] = "";
                        }else{
                            if($value['same_start_time'] < $current_time){
                                $examine_status = 1;
                            }
                            if($value['same_end_time'] < $current_time){
                                $examine_status = 2;
                            }
                            $list_data[$key]['examine_time'] = changeDate($value['same_start_time'])."---".changeDate($value['same_end_time']);
                        }
                    }else{
                        //高管
                        if($value['up_start_time'] == 0 || $value['up_end_time'] == 0){
                            $examine_status = 0;
                            $list_data[$key]['examine_time'] = "";
                        }else {
                            if ($value['up_start_time'] < $current_time) {
                                $examine_status = 1;
                            }
                            if ($value['up_end_time'] < $current_time) {
                                $examine_status = 2;
                            }
                            $list_data[$key]['examine_time'] = changeDate($value['up_start_time'])."---".changeDate($value['up_end_time']);
                        }
                    }
                }
            }

            $list_data[$key]['status'] = $examine_status;
            $list_data[$key]['status_str'] = $status_str_array[$examine_status];

            $list_data[$key]['add_time'] = changeDateTime($value['add_time']);

            //same_start_time,same_end_time,up_start_time,up_end_time
            unset($list_data[$key]['same_start_time']);
            unset($list_data[$key]['same_end_time']);
            unset($list_data[$key]['up_start_time']);
            unset($list_data[$key]['up_end_time']);
        }

        $return_data = array();
        $return_data['list_data'] = $list_data;

        $this->successReturn("获取成功",$return_data);
    }*/
}